package com.javayh.controller;

import com.javayh.bean.JsonDate;
import com.javayh.bean.UserAndRoleVo;
import com.javayh.entity.User;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.jboss.logging.Logger;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping("/user/")
@Api(value = "UserController",tags = "UserController")
public class UserController extends BaseContrllor {

	private Logger logger = Logger.getLogger(getClass().getName());

	/**
	  * 查询所有用户
	  * @author Dylan Yang
	  */
	@GetMapping("queryUsers")
	@ApiOperation(value = "查询所有用户",notes = "queryUsers")
	public JsonDate queryUsers() {
		List<User> userList = this.userSrevice.queryUsers();
		logger.info("userList ----> " + userList);
		if (userList.size()>0){
			return new JsonDate(200,"success",userList);
		}
		return new JsonDate(500,"error","系统内部错误请稍后再试!");
	}

	/**
	 * 查询所有用户及对应的角色
	 * @author Dylan Yang
	 */
	@PostMapping("queryUserAndRoleAll")
	@ApiOperation(value = "查询所有用户及对应的角色",notes = "queryUserAndRoleAll")
	public JsonDate queryUserAndRoleAll() {
		List<UserAndRoleVo> userList = this.userSrevice.queryUserAndRoleAll();
		if (userList.size()>0){
			return new JsonDate(200,"success",userList);
		}
		return new JsonDate(500,"error","系统内部错误请稍后再试!");
	}

	/**
	  * 根据姓名查询用户信息
	  * @author Dylan Yang
	  */
	@GetMapping("/findByName")
	@ApiOperation(value = "根据姓名查询用户信息",notes = "findByName")
	@ApiImplicitParam(name = "request",value = "2018-10-02",required = true,dataType = "HttpServletRequest")
	public JsonDate findByName(HttpServletRequest request) {
		String nickName = request.getParameter("name");
		if(StringUtils.isEmpty(nickName)){
			return new JsonDate(500,"error","请核实您数据的姓名!");
		}
		User user = userSrevice.findByName(nickName);
		if(user != null){
			return new JsonDate(200,"success",user);
		}
		return new JsonDate(500,"error","暂无您查找的用户!");
	}
}
